package CarManagement;

import CollegeInformation.*;

import javax.swing.*;
import java.util.List;

public class Service {
     static CarDao carDao = new CarDao();
//     添加车辆
     public static void addcar(){
         String id;
         String no = JOptionPane.showInputDialog(View2.contentPane,
                 "请选择你要添加的类型:1.大客车 2.小轿车 3.卡车"
                 ,"输入",JOptionPane.QUESTION_MESSAGE);
         if("1".equals(no)){
             while (true) {
                 id = JOptionPane.showInputDialog(View2.contentPane,
                         "请输入大客车编号：", "输入", JOptionPane.QUESTION_MESSAGE);
                 if (!Service.searchId(id)) {
                     JOptionPane.showMessageDialog(View2.contentPane, "已有该车辆,请重新输入ID!", "消息提示",
                             JOptionPane.INFORMATION_MESSAGE);
                     continue;
                 }else{
                     break;
                 }
             }
//            取消的BUG
             try {
                 if(id.length()<=0){
                     return;
                 }
             }catch(Exception e){
                 e.printStackTrace();
             }

//            弹出增加信息的对话框
             addBigCar dialog = new addBigCar(id);
             dialog.pack();
             dialog.setVisible(true);
             if(!searchId(id)){
                 //            弹出结果
                 JOptionPane.showMessageDialog(View2.contentPane, "添加成功", "消息提示",
                         JOptionPane.INFORMATION_MESSAGE);
             }

         }
         if("2".equals(no)) {
             while (true) {
                 id = JOptionPane.showInputDialog(View2.contentPane,
                         "请输入小轿车的编号：：", "输入", JOptionPane.QUESTION_MESSAGE);
                 if (!Service.searchId(id)) {
                     JOptionPane.showMessageDialog(View2.contentPane, "已有该车辆,请重新输入ID!", "消息提示",
                             JOptionPane.INFORMATION_MESSAGE);
                     continue;
                 } else {
                     break;
                 }
             }
             addSmallCar dialog = new addSmallCar(id);
             dialog.pack();
             dialog.setVisible(true);
             if(!searchId(id)) {
                 //            弹出结果
                 JOptionPane.showMessageDialog(View2.contentPane, "添加成功", "消息提示",
                         JOptionPane.INFORMATION_MESSAGE);
             }
         }
         if("3".equals(no)) {
             while (true) {
                 id = JOptionPane.showInputDialog(View2.contentPane,
                         "请输入卡车的编号：：", "输入", JOptionPane.QUESTION_MESSAGE);
                 if (!Service.searchId(id)) {
                     JOptionPane.showMessageDialog(View2.contentPane, "已有该车辆,请重新输入ID!", "消息提示",
                             JOptionPane.INFORMATION_MESSAGE);
                     continue;
                 } else {
                     break;
                 }
             }
             addTrack dialog = new addTrack(id);
             dialog.pack();
             dialog.setVisible(true);
             if(!searchId(id)) {
                 //            弹出结果
                 JOptionPane.showMessageDialog(View2.contentPane, "添加成功", "消息提示",
                         JOptionPane.INFORMATION_MESSAGE);
             }
         }
     }
    public static  void addFun1(String id, String license, String company, String buyTime, String type, int mile, float oilConsumption, int protectMoney, int roadMoney, float sum, int passengerNum){
        BigCar bigCar=new BigCar(id, license, company, buyTime,"大客车", mile, oilConsumption, protectMoney, roadMoney, sum, passengerNum);
        carDao.add(bigCar);
    }
    public static void addFun2(String id, String license, String company, String buyTime, String type, int mile, float oilConsumption, int protectMoney, int roadMoney, float sum, int boxNum){
        SmallCar smallCar=new SmallCar(id, license, company, buyTime, "小轿车", mile, oilConsumption, protectMoney, roadMoney, sum, boxNum);
        carDao.add(smallCar);
    }
    public static void addFun3(String id, String license, String company, String buyTime, String type, int mile, float oilConsumption, int protectMoney, int roadMoney, float sum, int weight){
        Track track=new Track(id, license, company, buyTime, "卡车", mile, oilConsumption, protectMoney, roadMoney, sum, weight);
        carDao.add(track);
    }
//显示所有车辆的信息
    public static void displayall(){
        List<Car> cars= carDao.displayall();
        String str = "";
        if(cars.size()==0){
            JOptionPane.showMessageDialog(View.contentPane, "不好意思，目前没有车辆信息可供查询，请回去重新输入你的选择", "消息提示",
                    JOptionPane.INFORMATION_MESSAGE);

        }else{
            for(Car car:cars){
                str += (car.toString() + "\n");

            }
            View.textArea.setText(str);
        }

    }
//    判断ID是否存在
    public static boolean searchId(String id){
//
        if( carDao.searchByid(id))//在数据库找不到符合该ID的教师，返回真
            return true;//不存在该ID的教师
        else
            return false;//存在该ID的教师

    }
//    根据选择来查询车辆
    public static void query(){
        String no = JOptionPane.showInputDialog(View2.contentPane,
                "请选择查询方式的编号(1/2/3)：1.按车辆制造公司查询 2.按编号查询 3.按类别查询"
                ,"输入",JOptionPane.QUESTION_MESSAGE);
        if("1".equals(no)){
            String companynname = JOptionPane.showInputDialog(View2.contentPane,
                    "请输入车辆制造公司的名称：", "输入", JOptionPane.QUESTION_MESSAGE);
            List<Car> cars = carDao.QueryCompany(companynname);
            if(cars.size()==0) {
                JOptionPane.showMessageDialog(View.contentPane, "没有该车辆,请重新输入编号!", "消息提示",
                        JOptionPane.INFORMATION_MESSAGE);
                return;
            }else{
                showCars dialog = new showCars(cars);
                dialog.pack();
                dialog.setVisible(true);}


        }
        if("2".equals(no)){
            String id = JOptionPane.showInputDialog(View2.contentPane,
                    "请输入你要查询车辆的编号"
                    ,"输入",JOptionPane.QUESTION_MESSAGE);
            if (Service.searchId(id)) {//数据库中不存在该ID
                JOptionPane.showMessageDialog(View2.contentPane, "没有该车辆,请重新输入编号!", "消息提示",
                        JOptionPane.INFORMATION_MESSAGE);
                return;
            } else {
                Car car = carDao.QueryId(id);
                showId dialog = new showId(car);
                dialog.pack();
                dialog.setVisible(true);
            }
        }
        if("3".equals(no)){
            String type = JOptionPane.showInputDialog(View2.contentPane,
                    "请输入车辆的类别：", "输入", JOptionPane.QUESTION_MESSAGE);
            List<Car> cars = carDao.QueryType(type);
            if(cars.size()==0) {
                JOptionPane.showMessageDialog(View.contentPane, "没有该车辆,请重新输入编号!", "消息提示",
                        JOptionPane.INFORMATION_MESSAGE);
                return;
            }else{
                showCars dialog = new showCars(cars);
                dialog.pack();
                dialog.setVisible(true);}
        }
        }
    //   根据ID 修改数据页面
    public static void showchange(Car car){
        updataCar dialog = new updataCar(car);
        dialog.pack();
        dialog.setVisible(true);
    }
//    修改车辆信息
    public static  void modify(Car car){
        carDao.modify(car);
        System.out.println("修改成功");
    }
    public static void delete(){
        String id;

        id = JOptionPane.showInputDialog(View.contentPane,
                "请输入要删除车辆的ID：", "输入", JOptionPane.QUESTION_MESSAGE);
        try {
            if (Service.searchId(id) && id.length() > 0) {//数据库中不存在该ID
                JOptionPane.showMessageDialog(View.contentPane, "没有该车辆,请重新操作!", "消息提示",
                        JOptionPane.INFORMATION_MESSAGE);
                return;
            } else {
                if (carDao.del(id))
                    JOptionPane.showMessageDialog(View.contentPane, "删除成功", "消息提示",
                            JOptionPane.INFORMATION_MESSAGE);
                else
                    JOptionPane.showMessageDialog(View.contentPane, "删除失败", "消息提示",
                            JOptionPane.INFORMATION_MESSAGE);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
//    统计数据
    public static void statistic(){
        Statistic  str=carDao.statistic();
        View.textArea.setText(str.toString());
    }

}
